package com.boss.salary.dao.impl;

import com.boss.framework.dao.BaseDaoImpl;
import com.boss.salary.constant.TableName;
import com.boss.salary.dao.IAutoSubmitDao;
import com.boss.salary.entity.AgencyVerify;
import com.boss.sys.utils.SysUtils;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Repository
public class AutoSubmitDaoImpl extends BaseDaoImpl implements IAutoSubmitDao {
    @Override
    public List<AgencyVerify> getNoReportAgencyData(String mofDivCode,List<String> withOutAgencyids) {
        StringBuilder sql = new StringBuilder();
        sql.append(" SELECT ");
        sql.append(" id, biz_key, create_time, update_time, agency_id, salary_month, salary_verified_in_agency_code, ")
                .append(" salary_info_verified_code, person_info_verified_code, agency_info_verified_code, is_submit_in_agency, ")
                .append(" is_salary_submit, is_person_submit, is_agency_submit, is_confirm_submit, is_month_end, is_deleted, ")
                .append(" audit_explain, remark, is_salary_change, add_person_num, add_quit_person_num, delete_person_num, ")
                .append(" is_thirteen, is_pay_data_created, is_bank_data_created, mof_div_code, year ");
        sql.append(" FROM ").append(TableName.AGENCY_VERIFY);
        sql.append(" WHERE mof_div_code = ? and is_month_end = 0  and is_salary_submit<>1 ");
        if(!CollectionUtils.isEmpty(withOutAgencyids)){
            sql.append(" and ").append(notInSql("agency_id", withOutAgencyids));
        }
        Object[] objects = new Object[] {
                mofDivCode
        };
        return this.getjdbcTemplate().query(sql.toString(), new BeanPropertyRowMapper<>(AgencyVerify.class), objects);
    }

    @Override
    public void updateAgencyInfo(AgencyVerify agencyVerify) {
        StringBuilder sql = new StringBuilder();
        sql.append(" UPDATE ").append(TableName.AGENCY_VERIFY);
        sql.append(" SET salary_verified_in_agency_code = ?, is_submit_in_agency = ?, is_salary_submit = ?, is_auto = ? ");
        sql.append(" WHERE id = ? ");
        Object[] objects = new Object[] {
                agencyVerify.getSalaryVerifiedInAgencyCode(), agencyVerify.getIsSubmitInAgency(),
                agencyVerify.getIsSalarySubmit(), agencyVerify.getIsAuto(), agencyVerify.getId()
        };
        this.getjdbcTemplate().update(sql.toString(), objects);
    }

    @Override
    public void bachUpdateAgencyInfo(List<AgencyVerify> agencyVerifys) {
        StringBuilder sql = new StringBuilder();
        sql.append(" UPDATE ").append(TableName.AGENCY_VERIFY);
        sql.append(" SET salary_verified_in_agency_code = ?, is_submit_in_agency = ?, is_salary_submit = ?, is_auto = ? ");
        sql.append(" WHERE id = ? ");
        List<Object[]> params=new ArrayList<>();
        for(AgencyVerify agencyVerify:agencyVerifys)
        params.add(new Object[] {
                agencyVerify.getSalaryVerifiedInAgencyCode(), agencyVerify.getIsSubmitInAgency(),
                agencyVerify.getIsSalarySubmit(), agencyVerify.getIsAuto(), agencyVerify.getId()
        });
        this.getjdbcTemplate().batchUpdate(sql.toString(),  new BatchPreparedStatementSetter() {
            public int getBatchSize() {
                return params.size();
                //这个方法设定更新记录数，通常List里面存放的都是我们要更新的，所以返回list.size();
            }
            public void setValues(PreparedStatement ps, int i)throws SQLException {
                Object[] sme = params.get(i);
                for(int a = 0; a < 5; a++){
                    ps.setObject(a+1,sme[a]);
                }

            }
        });
    }

    @Override
    public Map<String, Object> getWorkflowNodeConfig(String mofDivCode) {
        StringBuilder sql = new StringBuilder();
        sql.append(" SELECT process_def_key \"process_def_key\",first_menu_id \"first_menu_id\",user_id \"user_id\" ");
        sql.append(" FROM ").append(TableName.WORKFLOW_AUTO_REPORT_CONFIG);
        sql.append(" WHERE mof_div_code = ? ");
        return this.getjdbcTemplate().queryForMap(sql.toString(), new Object[]{mofDivCode});
    }

    public static String notInSql(String keyField, List<String> keyValues) {
        StringBuffer sb = new StringBuffer();
        if (SysUtils.isNullOrEmpty(keyValues)) {
            sb.append("(  ").append(keyField).append(" not in ('')) ");
        } else {
            sb.append("(  ").append(keyField).append(" not in ( ");

            for(int i = 0; i < keyValues.size(); ++i) {
                if (i % 1000 == 0 && i != 0) {
                    sb.deleteCharAt(sb.length() - 1);
                    sb.append(") or ").append(keyField).append(" not in ( ");
                }

                sb.append("'").append((String)keyValues.get(i)).append("',");
            }

            sb.deleteCharAt(sb.length() - 1);
            sb.append(") )");
        }

        return sb.toString();
    }
}
